#include "EditorMainWindowModel.h"
#include "Object/NewObject.h"


#include "Ability/Explorer/EditorExplorerAbilityGroupModel.h"
#include "Ability/DebugAndRun/EditorDebugAndRunAbilityGroupModel.h"

#include "Ability/EngineObject/EditorEngineObjectAbilityGroupModel.h"
#include "Ability/MaterialPreview/EditorMaterialPreviewAbilityModel.h"

#include "Framework/Application.h"

#include "PageArea/EditorPageAreaManagerModel.h"
#include "AbilityGroup/SideBar/EditorSideBarModel.h"
#include "AbilityGroup/Panel/EditorPanelModel.h"
#include "StatusBar/EditorStatusBarModel.h"
#include "Notification/EditorNotificationModel.h"

#include "Widgets/Splitable/EditorSplitableLayoutModel.h"


void REditorMainWindowModel::FirstTimeInitializeObject()
{

    Super::FirstTimeInitializeObject();

    //create default sub models
    MainSideBar=NewObject<REditorSideBarModel>(this);
    MainSideBar->ActiveBarPositionType=EEditorAbilityActiveBarPositionType::None;
    MainSideBar->ActivedAbilityIndex=0;
    auto Explorer = NewObject<REditorExplorerAbilityGroupModel>(this);
    MainSideBar->InsertAbility(0, Explorer);
    auto DebugAndRun = NewObject<REditorDebugAndRunAbilityGroupModel>(this);
    MainSideBar->InsertAbility(1, DebugAndRun);
    auto MaterialPreview = NewObject<REditorMaterialPreviewAbilityModel>(this);
    MainSideBar->InsertAbility(2, MaterialPreview);

    SecondarySideBar=NewObject<REditorSideBarModel>(this);
    SecondarySideBar->ActivedAbilityIndex=0;
    auto EngineObject = NewObject<REditorEngineObjectAbilityGroupModel>(this);
    SecondarySideBar->InsertAbility(0, EngineObject);

    Panel=NewObject<REditorPanelModel>(this);

    BodyLayout=NewObject<REditorSplitableLayoutModel>(this);
    //初始化布局树
    {
        MainSideBarNode=NewObject<REditorSplitableLayoutLeafNode>(this);
        MainSideBarNode->ItemWidgetModel=MainSideBar;
        MainSideBarNode->OnVisibleChanged.Bind(this,&REditorMainWindowModel::OnMainSideBarVisibleChangedBySpliter);

        PanelNode=NewObject<REditorSplitableLayoutLeafNode>(this);
        PanelNode->ItemWidgetModel=Panel;
        PanelNode->OnVisibleChanged.Bind(this,&REditorMainWindowModel::OnPanelVisibleChangedBySpliter);

        SecondarySideBarNode=NewObject<REditorSplitableLayoutLeafNode>(this);
        SecondarySideBarNode->ItemWidgetModel=SecondarySideBar;
        SecondarySideBarNode->OnVisibleChanged.Bind(this,&REditorMainWindowModel::OnSecondarySideBarVisibleChangedBySpliter);

        auto PageAreaNode=NewObject<REditorSplitableLayoutLeafNode>(this);
        PageAreaNode->ItemWidgetModel=GetPageArea();

        auto VerticalLayout=NewObject<REditorContainerSplitableLayoutNode>(this);
        VerticalLayout->SetVertical();
        VerticalLayout->AddChild(
            PageAreaNode
            , 600
            , 0
            , 200);
        VerticalLayout->AddChild(
            PanelNode
            , 200
            , 0
            , 200);
    
        auto Root=NewObject<REditorContainerSplitableLayoutNode>(this);
        Root->SetHorizontal();

        Root->AddChild(
            MainSideBarNode
            , 1
            ,0
            , 200);
    
        Root->AddChild(
            VerticalLayout
            , 3
            , 200
            , 200);

        Root->AddChild(
            SecondarySideBarNode
            , 1
            , 0
            , 200
            );

        BodyLayout->SetLayoutTree(Root);
    }

    StatusBar=NewObject<REditorStatusBarModel>(this);
    Notification=NewObject<REditorNotificationModel>(this);
}

TSharedObjectPtr<REditorSplitableLayoutModel> REditorMainWindowModel::GetBodyLayout() const
{
    return BodyLayout;
}

TSharedObjectPtr<REditorSideBarModel> REditorMainWindowModel::GetMainSideBar() const
{
    return MainSideBar;
}

void REditorMainWindowModel::ToggleMainSideBarVisible()
{
    bShowMainSideBar=!bShowMainSideBar;
    UpdateLayout();
}

bool REditorMainWindowModel::IsMainSideBarVisible() const
{
    return bShowMainSideBar;
}

TSharedObjectPtr<REditorSideBarModel> REditorMainWindowModel::GetSecondarySideBar() const
{
    return SecondarySideBar;
}

void REditorMainWindowModel::ToggleSecondarySideBarVisible()
{
    bShowSecondarySideBar=!bShowSecondarySideBar;
    UpdateLayout();
}

bool REditorMainWindowModel::IsSecondarySideBarVisible() const
{
    return bShowSecondarySideBar;
}

TSharedObjectPtr<REditorPanelModel> REditorMainWindowModel::GetPanel() const
{
    return Panel;
}

TSharedObjectPtr<REditorStatusBarModel> REditorMainWindowModel::GetStatusBar() const
{
    return StatusBar;
}

void REditorMainWindowModel::TogglePanelVisible()
{
    bShowPanel=!bShowPanel;
    UpdateLayout();
}

bool REditorMainWindowModel::IsPanelVisible() const
{
    return bShowPanel;
}

void REditorMainWindowModel::HidePanel()
{
    if(bShowPanel)
    {
        bShowPanel=false;
        UpdateLayout();
    }
}

TSharedObjectPtr<REditorNotificationModel> REditorMainWindowModel::GetNotification() const
{
    return Notification;
}

void REditorMainWindowModel::UpdateLayout()
{
    auto FormerLayoutTree=BodyLayout->GetLayoutRoot();
    assert(FormerLayoutTree);
   


    {
        //存储之前的布局大小
        TSharedObjectPtr<REditorContainerSplitableLayoutNode> 
            MainSideBarNodeParent=MainSideBarNode->Parent;
        auto IndexInParent=0;
        float FormerFrameSize=MainSideBarNodeParent->GetSubNodeSize(IndexInParent);
        
        //缓存非隐藏状态下的尺寸
        if(FormerFrameSize>0)
        {
            MainSideBarNodeWidth=FormerFrameSize;
        }

        //设置新的尺寸
        float NewSize=bShowMainSideBar?MainSideBarNodeWidth:0;
        float Delta=NewSize-FormerFrameSize;
        if(Delta!=0)
        {
            MainSideBarNodeParent->SetSubNodeSize(IndexInParent,NewSize);
            //调整页面区域节点的大小
            MainSideBarNodeParent->SetSubNodeSize(IndexInParent+1,MainSideBarNodeParent->GetSubNodeSize(IndexInParent+1)-Delta);
        }
        

    }

    {
        //存储之前的布局大小
        TSharedObjectPtr<REditorContainerSplitableLayoutNode> 
            PanelNodeParent=PanelNode->Parent;
        auto IndexInParent=1;
        float FormerFrameSize=PanelNodeParent->GetSubNodeSize(IndexInParent);
        
        //缓存非隐藏状态下的尺寸
        if(FormerFrameSize>0)
        {
            PanelNodeHeight=FormerFrameSize;
        }

        //设置新的尺寸
        float NewSize=bShowPanel?PanelNodeHeight:0;
        float Delta=NewSize-FormerFrameSize;
        if(Delta!=0)
        {
            PanelNodeParent->SetSubNodeSize(IndexInParent,NewSize);
            //调整页面区域节点的大小
            PanelNodeParent->SetSubNodeSize(IndexInParent-1,PanelNodeParent->GetSubNodeSize(IndexInParent-1)-Delta);
        }
    }

    {
        //存储之前的布局大小
        TSharedObjectPtr<REditorContainerSplitableLayoutNode> 
            SecondarySideBarNodeParent=SecondarySideBarNode->Parent;
        auto IndexInParent=2;
        float FormerFrameSize=SecondarySideBarNodeParent->GetSubNodeSize(IndexInParent);
        
        //缓存非隐藏状态下的尺寸
        if(FormerFrameSize>0)
        {
            SecondarySideBarNodeWidth=FormerFrameSize;
        }

        //设置新的尺寸
        float NewSize=bShowSecondarySideBar?SecondarySideBarNodeWidth:0;
        float Delta=NewSize-FormerFrameSize;
        if(Delta!=0)
        {
            SecondarySideBarNodeParent->SetSubNodeSize(IndexInParent,NewSize);
            //调整页面区域节点的大小
            SecondarySideBarNodeParent->SetSubNodeSize(IndexInParent-1,SecondarySideBarNodeParent->GetSubNodeSize(IndexInParent-1)-Delta);
        }
    }

    // {
    //     //存储之前的布局大小
    //     TSharedObjectPtr<REditorContainerSplitableLayoutNode> 
    //         PanelNodeParent=PanelNode->Parent;
    //     auto IndexInParent=1;
    //     if(PanelNodeParent->ChildrenSize[IndexInParent].Size>0)
    //     {
    //         PanelNodeHeight=PanelNodeParent->ChildrenSize[IndexInParent].Size;
    //     }

    //     //设置新的大小
    //     PanelNodeParent->ChildrenSize[IndexInParent].Size=bShowPanel?PanelNodeHeight:0;

    // }

    // {
    //     //存储之前的布局大小
    //     TSharedObjectPtr<REditorContainerSplitableLayoutNode> 
    //         SecondarySideBarNodeParent=SecondarySideBarNode->Parent;
    //     auto IndexInParent=2;
    //     if(SecondarySideBarNodeParent->ChildrenSize[IndexInParent].Size>0)
    //     {
    //         SecondarySideBarNodeWidth=SecondarySideBarNodeParent->ChildrenSize[IndexInParent].Size;
    //     }

    //     //设置新的大小
    //     float NewSize=bShowSecondarySideBar?SecondarySideBarNodeWidth:0;
    //     float Delta=NewSize-SecondarySideBarNodeParent->ChildrenSize[IndexInParent].Size;
    //     if(Delta!=0)
    //     {
    //         SecondarySideBarNodeParent->ChildrenSize[IndexInParent].Size=NewSize;
    //         //调整页面区域节点的大小
    //         SecondarySideBarNodeParent->ChildrenSize[IndexInParent-1].Size-=Delta;
    //     }
    // }

    //刷新布局
    BodyLayout->SetLayoutTree(FormerLayoutTree);
}

void REditorMainWindowModel::OnMainSideBarVisibleChangedBySpliter(bool NewVisible)
{
    bShowMainSideBar=NewVisible;
}

void REditorMainWindowModel::OnPanelVisibleChangedBySpliter(bool NewVisible)
{
    bShowPanel=NewVisible;
}

void REditorMainWindowModel::OnSecondarySideBarVisibleChangedBySpliter(bool NewVisible)
{
    bShowSecondarySideBar=NewVisible;
}
